<?php
/***
 * 产品模板分类
 *
 *
 * @author    lanhongjie <lanhj@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */

namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Controller;
use App\Services\ImgServices\ImgTool;
use App\Services\ProdTempCategoryService\ProdTempCategoryBackService;
use App\Services\ProdTempCategoryService\ProdTempCategoryTool;
use App\Services\ProductTempService\ProductTempTool;
use App\Services\Tool\GeneralTool;
use Illuminate\Http\Request;
class ProdTempCategoryController extends Controller
{
    protected  $prodTempCategorySer;
    protected  $generalTool;
    protected  $productTempTool;

    public function __construct(ProdTempCategoryBackService $prodTempCategorySer,
                                GeneralTool $generalTool,
                               ProductTempTool $productTempTool)
    {
        $this->prodTempCategorySer = $prodTempCategorySer;
        $this->generalTool = $generalTool;
        $this->productTempTool = $productTempTool;
    }

    /**
     * 获取产品类型列表
     * @param Request $request
     * @return $this
     */
    public function getProductCategoryList(Request $request)
    {
        $product_temp_categorys=  $this->prodTempCategorySer->getProdTempCategoryList($request);
        foreach ($product_temp_categorys as $temp_category){
            $temp_category->original_image=ImgTool::getImgUrlByImgPath($temp_category->original_image);
        }
        return view('backstage.prodTempCategory.prodTempCategoryList')->with('product_temp_categorys',$product_temp_categorys);
    }

    /**
     * 获取JSON类型列表
     * @param Request $request
     * @return $this
     */
    public function postJsonProductCategoryList(Request $request)
    {
        $productCategory =  $this->prodTempCategorySer->getProdTempCategoryList($request);
        return response()->json($productCategory);
    }

    /**
     * 跳转到添加类型页面
     * @return \Illuminate\View\View
     */
    public function getAddProductCategory()
    {
        return view('backstage.prodTempCategory.addProdTempCategory');
    }

    /**
     * 跳转到修改类型页面
     * @return \Illuminate\View\View
     */
    public function getUpdateProductCategory(Request $request)
    {
        $product_category_id = intval($request->input('id'));
        $product_category =  $this->prodTempCategorySer->getProductTempCategoryForId($product_category_id);
        if(empty($product_category))
        {
            return redirect()->back()->with('error_message', '类型不存在');
        }
        $product_category->original_image=ImgTool::getImgUrlByImgPath($product_category->original_image);
        return view('backstage.prodTempCategory.updateProdTempCategory')->with('product_category',$product_category);
    }

    /**
     * 添加类型
     * @return \Illuminate\View\View
     */
    public function postAddProductCategory(Request $request)
    {
        $name = trim($request->input('name'));
        $description = trim($request->input('description'));
        $original_image = $request->file('original_image');
        $productCategory = array('name'=>$name,'description'=>$description,'original_image'=>$original_image);
        $validator =  $this->prodTempCategorySer->addValidator($productCategory);
        $validator->after(function($validator) use ($name)
        {
            //判断除当前名称外有没有重复名称
            $b =  $this->prodTempCategorySer->getProductTempForNameIsNotCurrent(0,$name);
            if($b!=null)
            {
                $validator->errors()->add('name', '名称已存在!');
            }
        });
        if ($validator->fails())
        {
            $request->flash();
            return redirect()->back()->withErrors($validator->errors())->withInput();
        }

        $res=  $this->prodTempCategorySer->addProductTempCategory($productCategory);
        if($res)
        {
            return redirect('backstage/product-temp/product-temp-category-list')->with('success_message', '添加成功');
        }else{
            return redirect()->back()->with('error_message', '添加失败');
        }
    }

    /**
     * 修改类型
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function postUpdateProductCategory(Request $request)
    {
        $product_category_id =  intval($request->input('id'));
        $product_category = $this->prodTempCategorySer->getProductTempCategoryForId($product_category_id);
        if(empty($product_category))
        {
            return redirect()->back()->with('error_message', '类型不存在');
        }else{
            $name = trim($request->input('name'));
            $description = trim($request->input('description'));
            $original_image = $request->file('original_image');
            $productCategory = array('name'=>$name,'description'=>$description,'original_image'=>$original_image);
            $validator =  $this->prodTempCategorySer->updateValidator($productCategory);
            $validator->after(function($validator) use ($product_category_id,$name)
            {
                //判断除当前名称外有没有重复名称
                $b =  $this->prodTempCategorySer->getProductTempForNameIsNotCurrent($product_category_id,$name);
                if(!empty($b))
                {
                    $validator->errors()->add('name', '名称已存在!');
                }
            });
            if ($validator->fails())
            {
                $request->flash();
                return redirect()->back()->withErrors($validator->errors())->withInput();
            }
            $productCategory['id'] = $product_category_id;
            if(empty($productCategory['original_image'])){
                unset($productCategory['original_image']);
            }
            $num =  $this->prodTempCategorySer->updateProductTempCategory($productCategory);
            if($num>=1)
            {
                return redirect('backstage/product-temp/product-temp-category-list')->with('success_message', '修改成功');
            }else{
                return redirect()->back()->with('error_message', '修改失败');
            }
        }

    }

    /***
     * 删除产品模板分类
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delTempCategory(Request $request){
        $category_id=intval($request->input('id'));
        if(empty($category_id)){
            $result['state']=-2;
            $result['message']='分类Id为空';
            return response()->json($result);
        }
        $category_exist=ProdTempCategoryTool::productTempCategoryExistsByProdTempCategoryId($category_id);
        if(!$category_exist){
            $result['state']=-2;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $res_num=$this->prodTempCategorySer->deleteProductCtegorys($category_id);
        if($res_num>0){
            $result['state']=1;
            $result['message']='删除成功';
        }else{
            $result['state']=-1;
            $result['message']='删除失败';
        }
        return response()->json($result);
    }
    /**
     * 搜索产品模板分类
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function searchTempCategory(Request $request){
        $brands =  $this->prodTempCategorySer->searchProdTempCategoryList($request);
        $result=array('data'=>$brands,'state'=>1,'message'=>'获取成功');
        return response()->json($result);
    }
}